package com.ac.cv.client;

import com.ac.cv.client.service.MailService;
import com.ac.cv.client.service.MailServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MailFormatter extends PopupPanel {

	private MailServiceAsync mailService = GWT.create(MailService.class);

	
	public MailFormatter() {
		VerticalPanel panel = new VerticalPanel();
		Label senderLabel = new Label("Expéditeur :");
		Label subjectLabel = new Label("Titre :");
		Label messageLabel = new Label("Message :");
			
		final TextBox senderAdress = new TextBox();
		final TextBox subject = new TextBox();
		final TextArea content = new TextArea();
		HorizontalPanel hPanel = new HorizontalPanel();
		Button sendButton = new Button("Envoyer");
		Button cancelButton = new Button("Retour");
		hPanel.add(cancelButton);
		hPanel.add(sendButton);
		sendButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				mailService.sendMail(senderAdress.getText(), subject.getText(), content.getText(), new AsyncCallback<String>() {
					
					@Override
					public void onSuccess(String result) {
						Log.getInstance().log(result);
						MailFormatter.this.hide();
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Log.getInstance().log(caught.getMessage());
					}
				});
			}
			
		});
		
		cancelButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				MailFormatter.this.hide();
			}
			
		});
		
		panel.add(senderLabel);
		panel.add(senderAdress);
		panel.add(subjectLabel);
		panel.add(subject);
		panel.add(messageLabel);
		panel.add(content);
		panel.add(hPanel);
		this.add(panel);

	}

}
